package kr.ac.inha.vota;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;

public class SettingActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_setting, menu);
        return true;
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(event.getAction() == KeyEvent.ACTION_DOWN) {
    		if(keyCode == KeyEvent.KEYCODE_BACK) {
    			Intent intent = new Intent(SettingActivity.this, MainActivity.class);
    			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        		startActivity(intent);
        		finish();
        		
        		return false;
    		}
    	}
    	
    	// TODO Auto-generated method stub
    	return super.onKeyDown(keyCode, event);
    }
    
    public int TouchRect(Rect a, Rect b, Rect c, Rect d, MotionEvent event)
	{
		if(a.contains((int)event.getX(),(int)event.getY()))
			return 1;
		else if(b.contains((int)event.getX(),(int)event.getY()))
			return 2;
		else if(c.contains((int)event.getX(),(int)event.getY()))
			return 3;
		else if(d.contains((int)event.getX(),(int)event.getY()))
			return 4;
		else
			return -1;
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		Intent intent;
		
		super.onTouchEvent(event);
		
		Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        
		Rect a = new Rect();
		a.set((int)(0.806*display.getWidth()),(int)(0.358*display.getHeight()),
				(int)(0.927*display.getWidth()),(int)(0.43*display.getHeight()));
		Rect b = new Rect();
		b.set((int)(0.806*display.getWidth()),(int)(0.487*display.getHeight()),
				(int)(0.927*display.getWidth()),(int)(0.558*display.getHeight()));
		Rect c = new Rect();
		c.set((int)(0.806*display.getWidth()),(int)(0.616*display.getHeight()),
				(int)(0.927*display.getWidth()),(int)(0.688*display.getHeight()));
		Rect d = new Rect();
		d.set((int)(0.806*display.getWidth()),(int)(0.745*display.getHeight()),
				(int)(0.927*display.getWidth()),(int)(0.816*display.getHeight()));
		
		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			
		switch(TouchRect(a,b,c,d,event))
		{
		case 1:
			Toast.makeText(SettingActivity.this, "Microphone Test", Toast.LENGTH_SHORT).show();
			/*
			intent = new Intent(SettingActivity.this, OpicMainActivity.class);
    		startActivity(intent);
    		finish();
    		*/
			break;
		case 2:
			Toast.makeText(SettingActivity.this, "Volume", Toast.LENGTH_SHORT).show();
			/*
			intent = new Intent(SettingActivity.this, TeleMainActivity.class);
    		startActivity(intent);
    		finish();
    		*/
			break;
		case 3:
			Toast.makeText(SettingActivity.this, "HELP", Toast.LENGTH_SHORT).show();
			/*
			intent = new Intent(SettingActivity.this, VoicememosActivity.class);
    		startActivity(intent);
    		finish();
    		*/
			break;
		case 4:
			Toast.makeText(SettingActivity.this, "Manufacturer", Toast.LENGTH_SHORT).show();
			
			intent = new Intent(SettingActivity.this, SettingActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    		startActivity(intent);
    		finish();
			break;
		}   			
			return true;
		}
		return false;
	}
}
